package com.operation;

import java.util.Date;

import org.json.JSONObject;

import com.dataProcess.Noverify;
import com.enetiy.Nettimeout;
import com.service.AreausbleService;
import com.service.MapService;
import com.service.NetLinkService;
import com.threads.AddNetclickThread;
import com.threads.DelNettimeoutThread;
import com.threads.NetMapThread;
import com.util.Log4gUtil;

public class NET {
	public static  String msg;
	
	public  int time;
	public int mac_type;
	public int server_type;
	public int protectid;
	public String netOrder;
	public String orderid;
	
	/**
	 * 网络上线
	 * @param account
	 * @param accountPwd
	 * @param areaid
	 * @param ip
	 * @param bandwidth
	 * @param url
	 * @param duration
	 * @param gateIp
	 * @param userid
	 * @param mac
	 * @param phone
	 * @param server_type //上网类型:
	 * @param mac_type  //设备类型:1为 app,2为PC
	 * @param callid
	 * @param token
	 * @param areausbleService
	 * @return
	 */
	public  int netAllow( String account,  String accountPwd,  int areaid,  String ip,   int bandwidth,
			String url,   String duration,   String gateIp,   int userid,   String mac,   String phone,   int server_type,
			int mac_type,   String callid,   String token,  String apMac, String netOrder,
			MapService mapService ,  AreausbleService areausbleService, NetLinkService netLinkService){
		
		int ru = -2;
		String sendRequest="未进连网接口";
		if(mac==null){
			mac="";
		}
		if(mac_type==0){
			mac_type=1;
		}
		
		this.netOrder =100000000+userid+""+(100000000+(int)(Math.random()*100000000));
		//预约上网类型
		if(server_type==5){
			if(netOrder!=null&&!netOrder.equals("")){
				this.netOrder = netOrder;
			}
		}
		
		if (ip!=null&&!ip.equals("")){
				String param ="username="+account
						+"&password="+accountPwd
						+"&ip="+ip
						+"&protectid="+areaid
						+"&userid="+userid
						+"&mac="+mac
						+"&phone="+phone
						+"&duration="+duration
						+"&server_type="+ server_type
						+"&mac_type="+mac_type
						+"&orderid="+this.netOrder
						+"&NF_sessionid="+token
						+"&NF_callid="+callid;
				Log4gUtil.Info("netAllow-网络上线", userid+"", "url", url+"?"+param, callid, token);
				try {
					url = "http://"+gateIp+url;
					int num = 0;
					do {
						long startTime = new Date().getTime();
						
						HttpSendUrl send = new HttpSendUrl();
						sendRequest = send.getSendRequest(url, param,"POST",8000);
						
						if(!sendRequest.contains("****错误-")){
							JSONObject js = new JSONObject(sendRequest);
							if(js.has("code"))
								ru = js.getInt("code");
							if(js.has("msg"))
								msg = js.getString("msg");
							if(ru==1){
								//添加网络上线数
								AddNetclickThread addNetclickThread = new AddNetclickThread(userid, mac, ip, areaid, mac_type, server_type,
										areausbleService);
								Thread t= new Thread(addNetclickThread);
								t.start();
								//查是否有超时指令
								Noverify noverify =new Noverify();
								DelNettimeoutThread delNettimeoutThread = new DelNettimeoutThread(noverify, userid, ip, netLinkService);
								Thread thread = new Thread(delNettimeoutThread);
								thread.start();
								
								if(server_type==5){
									NetMapThread netMapThread = new NetMapThread(mapService, apMac, userid, areaid, duration,
											server_type,ip,mac,this.netOrder);
									Thread thread1 = new Thread(netMapThread);
									thread1.start();
								}
							}else if(ru==-1){
								//超时指令存入
								Nettimeout nettimeout = new Nettimeout(account, accountPwd, areaid, ip, mac, phone, 
										server_type, mac_type, new Date(), areaid, userid,apMac,duration,this.netOrder);
								Noverify  noverify =new Noverify();
								noverify.saveNettimeout(netLinkService, nettimeout);
							}
						}else{
							ru=-1;
						}
						num++;//循环次数
						if(num!=1){//不是第一次循环则延迟1秒
							Thread.sleep(1000);
						}
						Long endTime = new Date().getTime();
						Log4gUtil.Info("netAllow", userid+"", "第"+num+"上线时间", (startTime-endTime)+"-上线结果:"+sendRequest, callid, token);
						
					} while (ru!=1&&num<3);//连网未成功循环3次
				} catch (Exception e) {
					ru=-1;
				}
		}
	
		Log4gUtil.Info("netAllow-网络上线", userid+"", "上线时间", "-上线结果:"+sendRequest, callid, token);
		
		return ru;
	}
	
	/*public static void main(String[] args) {
		
		String ip ="192.168.120.3";
		String url =":8081/protal/close/toclosed.do";
		String gateIp = "139.199.72.236";
		String mac = "58-44-98-B1-DD-42";
		int userid=7922;
		
		netIntercept(20002, ip, url, gateIp, mac, userid, "", "", "");
	}
	*/
	//下线
	public  int  netIntercept(int areaid, String ip, String url,  String gateIp,  String mac, int userid, String phone,
			String callid,  String token,String netOrder){
		long startTime = new Date().getTime();
		int ru = -2;
		String sendRequest="未进网络下线接口";
		
		if(mac==null){
			mac="";
		}
		if (ip!=null&&!ip.equals("")){
			String param = "ip="+ip
					+"&protectid="+areaid
					+"&mac="+mac
					+"&userid="+userid
					+"&orderid="+netOrder
					+"&phone="+phone
					+"&NF_callid="+callid;
			Log4gUtil.Info("netIntercept-网络下线", userid+"", "param", param, callid, token);
			url= "http://"+gateIp+url;
			HttpSendUrl send = new HttpSendUrl();
		
			try {
				sendRequest = send.getSendRequest(url, param,"POST",8000);
				
				JSONObject js = new JSONObject(sendRequest);
				if(js.has("code"))
					ru = js.getInt("code");
				if(js.has("usertime"))
					time = js.getInt("usertime");
				if(js.has("msg"))
					msg = js.getString("msg");
				if(js.has("mac_type"))
					mac_type = js.getInt("mac_type");
				if(js.has("server_type"))
					server_type = js.getInt("server_type");
				if(js.has("protectid"))
					protectid = js.getInt("protectid");
				if(js.has("orderid"))
					orderid = js.getString("orderid");
			} catch (Exception e) {
				ru=-1;
			}
		}
		
		Long endTime = new Date().getTime();
		Log4gUtil.Info("netIntercept-网络下线输出", userid+"", "下线时间", (startTime-endTime)+"-下线结果:"+sendRequest, callid, token);
		return ru;
	}
}
